<?php
    /**
     * Created by PhpStorm.
     * User: feng
     * Date: 2017/3/6
     * Time: 18:26
     * explain: excel帮助类
     */

    namespace PHPExcel;

    /******************************************************
     * @example
     *  $options = [
     * 'nowsheet'=>0,
     * 'sheetName'=>'sheet1',
     * 'titleName'=>[
     * 'title1','title2','title3'
     * ],
     * 'value'=>[
     * 'arr1','arr2',
     * ],
     * 'outName'=>'abc.xls'
     * ];
     * ExcelHelper::getInstance()->init($options);
     * ExcelHelper::getInstance()->outPutExcel();
     ******************************************************/
    use Util\IntToChr;

    include 'PHPExcel.php';
    include 'PHPExcel/Writer/Excel2007.php';

    class ExcelHelper {
        /**
         * @var excel对象
         */
        private $excel;

        /**
         * @var 创建人
         */
        public $creater;
        /**
         * @var 最后修改人
         */
        public $lastMan;
        /**
         * @var 标题
         */
        public $title;
        /**
         * @var题目
         */
        public $subject;
        /**
         * @var描述
         */
        public $describe;
        /**
         * @var设置当前sheet
         */
        public $nowSheet;
        /**
         * @var sheet的名字
         */
        public $sheetName;
        /**
         * @var 表格头部数据
         */
        public $titleName;
        /**
         * @var 需要填充的值
         */
        public $value;
        /**
         * @var excel输出名字
         */
        public $outName;

        public static $instance = null;

        public static function getInstance ($arr = array ()) {
            if (is_null( self::$instance )) {
                self::$instance = new self;
            }

            return self::$instance;
        }

        /**
         * @param array $arr
         *
         * @return 初始化
         */

        public function init ($arr = array ()) {
            $this->excel = new \PHPExcel();
            if (isset( $arr )) {
                $this->nowSheet = isset( $arr['nowSheet'] ) ? $arr['nowSheet'] : 0;
                $this->sheetName = $arr['sheetName'];
                $this->titleName = $arr['titleName'];
                $this->value = $arr['value'];
                $this->outName = $arr['outName'];
            }
        }

        /**
         * 生成并输出excel表格
         */
        public function outPutExcel () {
            //创建人
            if (isset( $this->creater )) {
                $this->excel->getProperties()->setCategory( $this->creater );
            }
            //标题
            if (isset( $this->title )) {
                $this->excel->getProperties()->setTitle( $this->title );
            }
            //题目
            if (isset( $this->subject )) {
                $this->excel->getProperties()->setSubject( $this->subject );
            }
            //描述
            if (isset( $this->describe )) {
                $this->excel->getProperties()->setDescription( $this->describe );
            }
            //当前sheet
            if (isset( $this->nowSheet )) {
                $this->excel->setActiveSheetIndex( $this->nowSheet );
            }
            //sheet名称
            if (isset( $this->sheetName )) {
                $this->excel->getActiveSheet()->setTitle( $this->sheetName );
            }
            //设置第一行
            if (isset( $this->titleName )) {
                foreach ($this->titleName as $key => $value) {
                    $this->excel->getActiveSheet()->setCellValue( IntToChr::itoc( $key ) . 1, $value );
                }
            }
            $num = 0;

            if (isset( $this->value )) {
                foreach ($this->value as $k => $v) {
                    $num = $k + 2;
                    $in = 0;
                    foreach ($v as $ks => $vs) {
                        $this->excel->getActiveSheet()->setCellValue( IntToChr::itoc( $in ) . $num, $vs );
                        $in++;
                    }
                }
            }

            if (!isset( $this->outName )) {
                $this->outName = 'test.xls';
            }
            header( 'Content-Type: application/vnd.ms-excel' );
            header( 'Content-Disposition: attachment;filename="' . $this->outName . '"' );
            header( 'Cache-Control: max-age=0' );
            $objWriter = \PHPExcel_IOFactory::createWriter( $this->excel, 'Excel5' );
            header( "Content-Transfer-Encoding:binary" );
            $objWriter->save( 'php://output' );
            exit;
        }

    }
